<script lang="ts" setup>
import router from '@/router';
import axios from 'axios';
import { ref } from 'vue';
import { ElMessage } from 'element-plus'


let form = ref({
    username:"",
    password:""
})

async function onRegister(){
      if(form.value.username === "" || form.value.password === ""){
        ElMessage.error("用户名和密码不能为空")
        return
      }
      const res = await axios.post("http://localhost:8080/api/register",form.value)
      console.log(res.data)
      
      ElMessage({
        message: res.data.msg,
        type: 'success',
      })
      if(res.data.code === 200){
        router.push("/")
      }
      else{
        router.push("/register")
      }
}

</script>

<template>
    <h1>注册用户</h1>
    <div class="login-container">
      <el-form :model="form" label-width="60px">
          <el-form-item label="用户名">
            <el-input v-model="form.username" />
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="form.password" type="password"/>
          </el-form-item>
          <el-form-item>
            <el-button @click="onRegister">注册</el-button>
          </el-form-item>
      </el-form>
    </div>
</template>

<style lang="css" scoped>
    .login-container {
      display: flex;
      justify-content: center; /* 水平居中 */
      align-items: center; /* 垂直居中 */
      height: 50vh; /* 设置容器高度为视口高度 */
    }
    h1{
      text-align: center;
    }
</style>